﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class D02_GameObject : MonoBehaviour
{
    public Transform prefab;//预制体

    //演示实例化
    private void Start1()
    {
        //monster是实例化之后的物体
        Transform monster = Instantiate(prefab, new Vector3(0, 0, 1), Quaternion.identity);
    }

    //演示销毁
    private void Start2()
    {
        //Destroy(this); //销毁此组件对象 (移除组件)
        Destroy(this.gameObject);//销毁此组件所在的游戏物体
    }

    //加载场景时不销毁(用的比较少了)
    private void Start3()
    {
        DontDestroyOnLoad(Camera.main.gameObject);
    }

    //演示根据名称查找,注意:1不能查找被禁用的物体,2查找消耗性能一般不在游戏中途查找
    private void Start4()
    {
        //找到灯光物体
        GameObject go = GameObject.Find("Directional Light");
        //修改灯光颜色
        go.GetComponent<Light>().color = Color.red;
    }

    //根据标签查找 游戏物体
    private void Start5()
    {
        //找到标签是A的游戏物体   需要字节确认只有一个物体使用A标签
        GameObject go = GameObject.FindGameObjectWithTag("A");
        go.GetComponent<MeshRenderer>().material.color = Color.red;

        //找到标识是B的所有游戏物体
        GameObject[] gos = GameObject.FindGameObjectsWithTag("B");
        for (int i = 0; i < gos.Length; i++)
        {
            gos[i].GetComponent<MeshRenderer>().material.color = Color.green;
        }
    }

    //演示根据类型查找
    private void Start6()
    {
        //找到场景中的唯一的灯光组件
        //Light light = FindObjectOfType<Light>(); 泛型
        Light light = FindObjectOfType(typeof(Light)) as Light;//type
        light.color = Color.red;

        // //找到场景中所有Tranform组件  销毁其所在的游戏物体
        //
        // Transform[] array = FindObjectsOfType<Transform>();
        //
        // for (int i = 0; i < array.Length; i++)
        // {
        //     Destroy(array[i].gameObject);
        // }
    }

    //演示获取组件
    private void Start7()
    {
        // MeshRenderer renderer = this.gameObject.GetComponent<MeshRenderer>();
        MeshRenderer renderer = GetComponent<MeshRenderer>();
    }

    //演示找到Cube并获取它身上的TestComp组件,并修改postion变量的值
    public GameObject cube;
    private void Start8()
    {
        //   cube.GetComponent<TestComp>().postion = new Vector3(2, 2, 2);
        (cube.GetComponent("TestComp") as TestComp).postion = new Vector3(3, 3, 3);
        (cube.GetComponent(typeof(TestComp)) as TestComp).postion = new Vector3(5, 5, 5);
    }

    //演示添加组件
    private void Start9()
    {
        //Rigidbody rig = cube.AddComponent<Rigidbody>();
        Rigidbody rig = cube.AddComponent(typeof(Rigidbody)) as Rigidbody;
    }

    //演示SendMessage调用方法
    private void Start10()
    {
        //正常调用    拿到物体->拿到组件->调用方法
        //cube.GetComponent<TestComp>().Fun();

        //SendMessage   拿到物体->调用方法
        cube.SendMessage("Fun");
    }

    //演示属性
    private void Start11()
    {
        print(cube.name);
        print(cube.tag);
        print(cube.activeSelf);//自己的激活状态
        print(cube.activeInHierarchy);//层级面板中的激活状态(有父物体决定)
    }

    //演示延迟调用  和重复调用
    private void Start()
    {
        //Invoke("Test", 5);

        InvokeRepeating("Test", 0, 1);
    }

    //演示去掉调用任务
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))//空格取消
        {
        }
    }

    private void OnDisable()
    {
        CancelInvoke("Test");
    }

    private void Test()
    {
        print("Test");
    }
}